from zhipuai import ZhipuAI
import pandas as pd
import re

if __name__ == '__main__':
    # 创建ZhipuAI对象
    client = ZhipuAI(api_key="d30a6ff4270323ffd9a296814026bb85.Z4eqtU452FLIG6Cf")  # 填写您自己的APIKey
    # 读取数据
    data = pd.read_excel('西安金融·张可心-7407731780746366258.xlsx')
    # 遍历数据，调用接口
    for index, row in data.iterrows():
        # 调用接口
        pattern = r'\[.*?\]|{.*?}|\(.*?\)'  # 匹配小括号、中括号、大括号及其内容
        comment_text = re.sub(pattern, '', row['评论'])  # 替换匹配到的内容为空字符串
        if comment_text:
            response = client.chat.completions.create(
                model="glm-4-flash",
                messages=[
                    {
                        "role": "system",
                        "content": "你是一个资深的信贷销售专员，分析语句是否有做贷款的需求，只回答是或否"
                    },
                    {
                        "role": "user",
                        "content": comment_text
                    }
                ],
                # top_p=0.7,
                # temperature=0.95,
                # max_tokens=1024,
                # tools=[{"type": "web_search", "web_search": {"search_result": True}}],
                # stream=False
            )

            row['分析'] = response.choices[0].message.content.strip()
            print(row)
